﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Models;
using WindowsFormsApp1.Respostories;
using WindowsFormsApp1.Utils;


namespace WindowsFormsApp1
{
    public partial class EditUserFrom : Form
    {
        public User user= null;
        public List<Role> roles = new List<Role>();

        public EditUserFrom()
        {
            InitializeComponent();
        }
        public EditUserFrom(User user) : this()
        {
            this.user = user;
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btSave_Click(object sender, EventArgs e)
        {
            bool isEdit = user!= null;//判断是否为编辑操作,ture为编辑操作，false为新增操作

            //使用正则表达式判断手机号是否为11位数字
            Regex phonePattern = new Regex(@"^1[3-9][0-9]{9}$");
            //数据有效性判断
            if (this.txtName.Text == "")
            {
                this.lbNameWarn.Text = "请输入有效姓名！";
                //系统响铃提示一下
                System.Media.SystemSounds.Asterisk.Play();
                return;
            }
            else if (this.txtMobile.Text == "" || !phonePattern.IsMatch(this.txtMobile.Text))
            {
                this.lbMobileWarn.Text = "请输入有效手机号码！";
                System.Media.SystemSounds.Asterisk.Play();
                return;
            }
            else if (this.cbState.Text == "")
            {
                this.lbRoleNameWarn.Text = "请选择用户角色！";
                System.Media.SystemSounds.Asterisk.Play();
                return;
            }
           
                //数据有效性通过，保存数据
                if (!isEdit)
                {
                    user=new User();
                    user.Id = Guid.NewGuid().ToString();//新增用户，设置Id
                }
                else
                {
                    //user.Id=this.user.Id;
                }
                user.Name = this.txtName.Text;
                user.Mobile = this.txtMobile.Text;
                //默认密码是手机号码后四位,
                user.Password = this.txtMobile.Text.Substring(7, 4);
                //根据字典获取角色id
                user.RoleId = cbRoleName.SelectedIndex;
                //判断cbState是否选中
                if( this.cbState.Checked==true )
                {
                    user.State = 1;
                }
                else
                {
                    user.State = 0;
                }
                user.RoleName = this.cbRoleName.SelectedItem.ToString();
            
            

            //判断是编辑还是新增
            if (isEdit)//编辑操作
            {
                bool isSuccess = UserRespostoriy.UpdateUser(this.user);
                if (isSuccess)
                {
                    //返回ok
                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("编辑用户成功！", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("编辑用户失败！","提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }
            else//新增操作
            {
                bool isSuccess = UserRespostoriy.AddUser(user);
                if (isSuccess)
                {
                    MessageBox.Show("新增用户成功！" ,"提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    //返回ok
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("新增用户失败！","提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }

            }




        }

        /// <summary>
        /// 编辑用户窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditUserFrom_Load(object sender, EventArgs e)
        {
            //获取角色列表
            //分类信息加载两种方式
            // 加载分类信息
            try
            {
                this.roles = RoleRespostoriy.GetRoles();
                this.roles.Insert(0, new Role
                {
                    Id = 0,
                    Name = "请选择..."
                });
                this.cbRoleName.DataSource = this.roles;
                this.cbRoleName.DisplayMember = "Name";
                this.cbRoleName.ValueMember = "Id";
                // 指定选中项
                // 加载图书信息, 如果不为空，说明是编辑，否则为新增操作
                if (null != this.user)
                {
                    this.txtName.Text = user.Name;
                    this.txtMobile.Text = user.Mobile;
                    this.cbRoleName.SelectedValue = this.user.RoleId;
                    //this.cbRoleName.SelectedIndex = this.user.RoleId;
                }
                else
                {
                    this.cbRoleName.SelectedIndex = 0;
                    this.Text = "新增用户";
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("加载角色信息失败:" + ex.Message);
            }
        }
        
        private void btCancel_Click(object sender, EventArgs e)
        {
            //关闭窗口
            this.Close();
        }
    }
}
